package com.ruoyi.common.file.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.util.Base64;

/**
 * ClassName：SignatureUtils
 * Package：com.ruoyi.common.oss.util
 * Description：签名处理工具类
 *
 * @Author：fpy
 * @Create：2025/5/6 - 16:11
 * @Version：v1.0
 */
public class SignatureUtils {

    /**
     * 生成签名
     * 华为云-对象存储服务 OBS
     * @param: sk
     * @param: canonicalString
     * @return: java.lang.String
     * @author: fpy
     * @date: 2025/5/6 16:12
    **/
    public static String signWithHmacSha1(String sk, String canonicalString) throws UnsupportedEncodingException {

        try {
            SecretKeySpec signingKey = new SecretKeySpec(sk.getBytes("UTF-8"), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            return Base64.getEncoder().encodeToString(mac.doFinal(canonicalString.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
